﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using InHD.model;
using System.Windows.Input;
using InHD.dataaccess;
using System.Windows.Controls;
using System.Collections.ObjectModel;
using InHD.commands;
using System.Windows;
using InHD.Properties;
using InHD.utilities.enums;

namespace InHD.viewmodels
{
    class CategoryViewModel: WorkspaceViewModel
    {
        #region Constructor
        public CategoryViewModel()
        {
            this._categoryRepository = new CategoryRepository();
            this._category = Category.CreateCategory();
            LoadCategoryList();
            this.PageContent = new CategoriesListViewModel(this._categoryRowCollection);
            this.PageContent.RequestClose += this.HandleCloseCurrentPage;
        }
        #endregion

        #region Value Property
        private Category _category;
        readonly CategoryRepository _categoryRepository;
        private WorkspaceViewModel _pageContent;
        private List<Category> _categoryRowCollection = new List<Category>();
        private string _displayName;
        #endregion

        #region Presentation Property

        public WorkspaceViewModel PageContent
        {
            get
            {
                return _pageContent;
            }
            set
            {
                _pageContent = value;
            }
        }

        public override string DisplayName
        {
            get
            {
                try
                {
                    return Resources.Title_Category;
                }
                catch (Exception ex)
                {
                    return string.Empty;
                }
            }

        }

        #endregion

        #region Normal Method
        private void LoadCategoryList()
        {
            this._categoryRepository.LoadAllCategories();
            this._categoryRowCollection = _categoryRepository.GetCategory();
        }

        private void HandleCloseCurrentPage(object source, EventArgs e)
        {
            this.PageContent.RequestClose -= this.HandleCloseCurrentPage;
            if (this.PageContent is CategoriesListViewModel)
            {
                string commandExcuted = ((CategoriesListViewModel)PageContent).commandExcute;
                if (string.IsNullOrEmpty(commandExcuted))
                {
                    OnRequestClose();
                    return;
                }

                if (commandExcuted.Equals("View"))
                {
                    this._category = null;
                    this._category = ((CategoriesListViewModel)PageContent).SelectedCategory.CreateShallowCopy();
                    this.PageContent = new CategoryFormViewModel(_category);
                    this.PageContent.RequestClose += this.HandleCloseCurrentPage;

                    OnPropertyChanged("PageContent");
                }
                else if (commandExcuted.Equals("Add"))
                {
                    this._category = Category.CreateCategory();
                    this.PageContent = new CategoryFormViewModel(_category);
                    this.PageContent.RequestClose += this.HandleCloseCurrentPage;
                    OnPropertyChanged("PageContent");
                }
            }
            else if (this.PageContent is CategoryFormViewModel)
            {
                string commandExcuted = ((CategoryFormViewModel)PageContent).commandExcute;
                if (commandExcuted.Equals("Close"))
                {
                    this._category = null;
                    this.PageContent = new CategoriesListViewModel(_categoryRowCollection);
                    this.PageContent.RequestClose += this.HandleCloseCurrentPage;           
                    OnPropertyChanged("PageContent");
                }
                else if (commandExcuted.Equals("Save"))
                {
                    if (((CategoryFormViewModel)PageContent).FormMode.Equals(ActionModes.New))
                    {
                        if (string.IsNullOrEmpty(this._categoryRepository.AddCategory(((CategoryFormViewModel)PageContent).Category)))
                        {
                            this._categoryRowCollection = null;
                            this._categoryRowCollection = _categoryRepository.GetCategory();
                        }
                        else
                        {
                            MessageBox.Show(Resources.InHD_DB_Error);
                        }
                    }
                    else
                    {
                        if (string.IsNullOrEmpty(this._categoryRepository.EditCategory(((CategoryFormViewModel)PageContent).Category)))
                        {
                            this._categoryRowCollection = null;
                            this._categoryRowCollection = _categoryRepository.GetCategory();
                        }
                        else
                        {
                            MessageBox.Show(Resources.InHD_DB_Error);
                        }
                    }

                    this.PageContent = new CategoriesListViewModel(_categoryRowCollection);
                    this.PageContent.RequestClose += this.HandleCloseCurrentPage;
                    OnPropertyChanged("PageContent");
                }
                else if (commandExcuted.Equals("Delete"))
                {
                    if (string.IsNullOrEmpty(this._categoryRepository.DeleteCategory(((CategoryFormViewModel)PageContent).Category)))
                    {
                        this._categoryRowCollection = null;
                        this._categoryRowCollection = _categoryRepository.GetCategory();
                    }
                    else
                    {
                        MessageBox.Show(Resources.InHD_DB_Error);
                    }

                    this.PageContent = new CategoriesListViewModel(_categoryRowCollection);
                    this.PageContent.RequestClose += this.HandleCloseCurrentPage; 
                    OnPropertyChanged("PageContent");
                }
            }
        }
        #endregion
    }
}
